package com.uliky.shouyiLibrary.ware.listener;

import com.alibaba.fastjson.TypeReference;
import com.uliky.common.constant.CartConstant;
import com.uliky.common.to.mq.OrderTo;
import com.uliky.common.to.mq.SeckillOrderTo;
import com.uliky.common.to.mq.StockDetailTo;
import com.uliky.common.to.mq.StockLockedTo;
import com.uliky.common.utils.R;
import com.uliky.shouyiLibrary.ware.entity.WareOrderTaskDetailEntity;
import com.uliky.shouyiLibrary.ware.entity.WareOrderTaskEntity;
import com.uliky.shouyiLibrary.ware.feign.OrderFeignService;
import com.uliky.shouyiLibrary.ware.service.WareOrderTaskDetailService;
import com.uliky.shouyiLibrary.ware.service.WareOrderTaskService;
import com.uliky.shouyiLibrary.ware.service.WareSkuService;
import com.uliky.shouyiLibrary.ware.vo.OrderVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "stock.release.stock.queue")
public class StockReleaseListener {
    @Autowired
    OrderFeignService orderFeignService;
    @Autowired
    WareSkuService wareSkuService;
    /**
     * 消息处理  --库存解锁
     * 下单成功，库存锁定成功，订单回滚，库存解锁
     * @param stockLockedTo
     * @param message
     * @return
     */
    @RabbitHandler
    public void handlerStockLockRelease(StockLockedTo stockLockedTo, Message message , Channel channel) throws IOException {
        try {
            wareSkuService.unlockStock(stockLockedTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
    /**
     * 订单完成
     */
    @RabbitHandler
    public void handlerOrderCloseRelease(OrderTo order, Message message , Channel channel) throws IOException {
        try {
            wareSkuService.unlockStock(order);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }

}
